package com.example.jiangkt.ergongyi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.jiangkt.ergongyi.R;
import com.example.jiangkt.ergongyi.entity.RoomInfo;

import java.util.List;

/**
 * Created by jiangkt on 2018/3/12.
 */

public class RoomListAdapter extends BaseAdapter {
    private Context context;
    private List<RoomInfo> roomInfoList;

    public RoomListAdapter(Context context, List<RoomInfo> roomInfoList){
        this.context = context;
        this.roomInfoList = roomInfoList;
    }

    @Override
    public int getCount() {
        return roomInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return roomInfoList.get(i).getRoomName();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    /**
     * 再次优化后的ListView
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view==null) {
//            view = View.inflate(context, R.layout.room_info, null);
            //和上面那个方式比，不知道那种方式较好
            view = LayoutInflater.from(context).inflate(R.layout.room_info,viewGroup,false);
            holder = new ViewHolder();
            holder.roomName = view.findViewById(R.id.tv_room_name);
            holder.userLimit = view.findViewById(R.id.tv_user_limit);
            holder.userUpper = view.findViewById(R.id.tv_user_upper);
            holder.userLower = view.findViewById(R.id.tv_user_lower);
            holder.roomStatus = view.findViewById(R.id.tv_room_status);
            view.setTag(holder);//将Holder存储到convertView中
        }else {
            holder = (ViewHolder) view.getTag();
        }

        holder.roomName.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.userLimit.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.userUpper.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.userLower.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.roomStatus.setTextColor(context.getResources().getColor(R.color.colorWhite));
        holder.roomName.setText("房间名称："+roomInfoList.get(i).getRoomName().split("-")[0]);
        holder.userLimit.setText("玩家最多人数："+roomInfoList.get(i).getUserLimit());
        holder.userUpper.setText("压筹上限："+roomInfoList.get(i).getUpperChip());
        holder.userLower.setText("压筹下限："+roomInfoList.get(i).getLowerChip());
        holder.roomStatus.setText("1".equals(roomInfoList.get(i).getRoomStatus())?"等待加入":"房间已满");
        return view;
    }

    static class ViewHolder{
        TextView roomName;
        TextView userLimit;
        TextView userUpper;
        TextView userLower;
        TextView roomStatus;
    }
}
